package com.android.xunxian.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.xunxian.HomeActivity;
import com.android.xunxian.R;
import com.android.xunxian.utils.UConstants;
import com.android.xunxian.utils.Util;


/**
 * 设置保存服务器
 * 
 * @author -zouqh
 * 
 */
public class SaveServiceActivity extends Activity implements OnClickListener {

	private ImageView close;
	private TextView title;
    private EditText ip;
    private EditText post;
    private Button submit;
    String tpye;
    Util util;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.save_service_activity);
		close = (ImageView) findViewById(R.id.close);
		title = (TextView) findViewById(R.id.title_text);
        ip=(EditText)findViewById(R.id.ip);
        post=(EditText)findViewById(R.id.post);
        submit=(Button)findViewById(R.id.submit);
		title.setText("设置服务器");
        tpye=getIntent().getStringExtra("type");
		close.setOnClickListener(this);
        submit.setOnClickListener(this);
        util=new Util(this);
        String strIp=util.getUser("ip");
        String strPost=util.getUser("post");
        if (Util.NoNull(strIp)){
            ip.setText(strIp);
        }
        if (Util.NoNull(strPost)){
            post.setText(strPost);
        }
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		Intent intent ;
		switch (v.getId()) {
		case R.id.close:
			finish();
			break;

            case R.id.submit:
                if (ip.getText().toString().trim().length()<=0){
                    Toast.makeText(this,"请输入ip地址",Toast.LENGTH_SHORT).show();
                }else  if (post.getText().toString().trim().length()<=0){
                    Toast.makeText(this,"请输入端口号",Toast.LENGTH_SHORT).show();
                }else{
                    UConstants.WEB_URL="http://"+ip.getText().toString().trim()+":"+post.getText().toString().trim()+"/BLL/";

                    util.setUser(ip.getText().toString().trim()+"",post.getText().toString().trim()+"");
                    if (tpye.equals("1")){
                        new UConstants().setIp();
                        finish();
                    }else{
                        HomeActivity.mainActivity.finish();
                        SettingActivity.activity.finish();
                        Toast.makeText(getApplicationContext(),"重启程序！",10).show();
                        finish();
                    }
                }
                break;
			
		default:
			break;
		}
	}


}
